﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;

namespace YJKDataBase
{
	public class SqlServerObjectFileTable : IObjectFileTable
	{
		public SqlServerObjectFileTable(SqlServerDatabase db)
		{
			this.mDatabase = db;
		}

		public void Update()
		{
		}

		public bool Add(DbObject dbObject, FileInfo fi, DbTransaction tr)
		{
			byte[] value = Assist.ReadFile(fi.FullName);
			bool result;
			try
			{
				using (SqlCommand sqlCommand = ((SqlConnection)this.mDatabase.dbCon).CreateCommand())
				{
					sqlCommand.Transaction = (SqlTransaction)tr;
					sqlCommand.CommandText = string.Format("insert into [ObjectFileTable] ([ObjectGUID],[ObjectFile]) values ('{0}',{1})", dbObject.Guid.ToString(), "@file");
					SqlParameter sqlParameter = new SqlParameter("@file", DbType.Binary);
					sqlParameter.Value = value;
					sqlCommand.Parameters.Add(sqlParameter);
					sqlCommand.ExecuteNonQuery();
				}
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public bool Remove(DbObject dbObject, DbTransaction tr)
		{
			bool result;
			try
			{
				this.mDatabase.ExecuteCommand("delete from [ObjectFileTable] where [ObjectGUID]='" + dbObject.Guid.ToString() + "'", tr);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public FileInfo this[DbObject dbObject, string exeName]
		{
			get
			{
				string text = Assembly.GetExecutingAssembly().Location;
				int num = text.LastIndexOf("\\");
				text = text.Substring(0, num + 1);
				FileInfo result = null;
				DataTable dataTable = this.mDatabase.ExecQuery("select [ObjectFile] from [ObjectFileTable] where [ObjectGUID]='" + dbObject.Guid.ToString() + "'");
				DataTable dataTable2 = this.mDatabase.ExecQuery("select [Name] from [ObjectTable] where [GUID]='" + dbObject.Guid.ToString() + "'");
				if (dataTable != null && dataTable2 != null)
				{
					byte[] pReadByte = (byte[])dataTable.Rows[0][0];
					string fileName = string.Concat(new string[]
					{
						text,
						"\\",
						dataTable2.Rows[0][0].ToString(),
						".",
						exeName
					});
					Assist.WriteFile(pReadByte, fileName);
					result = new FileInfo(fileName);
				}
				return result;
			}
		}

		public bool Replace(DbObject dbObject, FileInfo fi)
		{
			DbTransaction dbTransaction = this.mDatabase.StartTransaction();
			byte[] value = Assist.ReadFile(fi.FullName);
			bool result;
			try
			{
				using (SqlCommand sqlCommand = ((SqlConnection)this.mDatabase.dbCon).CreateCommand())
				{
					sqlCommand.Transaction = (SqlTransaction)dbTransaction;
					sqlCommand.CommandText = string.Format("update [ObjectFileTable] set [ObjectFile]={0} where [ObjectGUID]='{1}'", "@file", dbObject.Guid.ToString());
					SqlParameter sqlParameter = new SqlParameter("@file", DbType.Binary);
					sqlParameter.Value = value;
					sqlCommand.Parameters.Add(sqlParameter);
					sqlCommand.ExecuteNonQuery();
				}
				dbTransaction.Commit();
				result = true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private SqlServerDatabase mDatabase;
	}
}
